package com.deanlib.cabinet.ui.view.photo;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;

import com.deanlib.cabinet.R;
import com.deanlib.cabinet.base.BaseFragment;
import com.deanlib.cabinet.data.model.Resource;
import com.deanlib.cabinet.data.network.NetWorkManager;
import com.deanlib.cabinet.databinding.FragmentPhotoList2Binding;
import com.deanlib.cabinet.ui.adapter.MediaGrid2Adapter;
import com.deanlib.cabinet.utils.UserManager;
import com.deanlib.plantern.utils.PopupUtils;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Dean on 2022/12/11
 */
public class PhotoList2Fragment extends BaseFragment<FragmentPhotoList2Binding> {

    MediaGrid2Adapter mediaGridAdapter;
    List<Resource> mediaGridDataList;
    int pageSize = 30;
    int pageNum = 1;

    @Override
    public void initView() {
        binding.rvResources.setLayoutManager(new GridLayoutManager(getContext(), 3));
        mediaGridAdapter = new MediaGrid2Adapter(mediaGridDataList = new ArrayList<>());
        binding.rvResources.setAdapter(mediaGridAdapter);
//        binding.rvResources.addItemDecoration(new PinnedHeaderItemDecoration(getContext()));

        binding.srLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                pageNum ++;
                loadData();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pageNum = 1;
                mediaGridDataList.clear();
                loadData();
            }
        });
    }

    @Override
    public void loadData() {
        if (UserManager.getInstance().isLogin()) {
            request(NetWorkManager.getRequest().getResourceList(pageNum, pageSize), optional -> {
                if (optional.isPresent() && !optional.get().isEmpty()) {
//                    int position = mediaGridDataList.size();
                    mediaGridDataList.addAll(optional.get());
//                    System.out.println("dfddfdfdf" + position + "  dkfjldkjfld:" + optional.get().size());
//                    mediaGridAdapter.notifyItemRangeChanged(position, optional.get().size());
                    mediaGridAdapter.notifyDataSetChanged();
                    finishRefreshLayout(false);
                } else {
                    finishRefreshLayout(true);
                }
            }, err -> {
                PopupUtils.sendToast(err.getMessage());
                finishRefreshLayout(false);
            });
        }
    }

    private void finishRefreshLayout(boolean noMoreData){
        if (binding.srLayout.isRefreshing()){
            if (noMoreData){
                binding.srLayout.finishRefreshWithNoMoreData();
            }else {
                binding.srLayout.finishRefresh();
            }
        }else if (binding.srLayout.isLoading()){
            if (noMoreData){
                binding.srLayout.finishLoadMoreWithNoMoreData();
            }else {
                binding.srLayout.finishLoadMore();
            }
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_photo_list2;
    }
}
